package com.schoolET.controller.admin;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.DesensitizedUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.schoolET.entity.Wechat;
import com.schoolET.entity.dto.WechatInfoDTO;
import com.schoolET.entity.vo.WechatInquireVO;
import com.schoolET.error.costom.NoWechatException;
import com.schoolET.error.costom.PermissionsException;
import com.schoolET.result.MyPage;
import com.schoolET.result.Result;
import com.schoolET.service.admin.IWechatService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: lin
 * @className: WechatManageController
 * @createTime: 2024-06-04  15:06
 * @description: 后台微信用户管理
 */
@Api(tags = "后台微信用户管理")
@RestController
@RequestMapping("/schoolET")
public class WechatManageController {

    @Autowired
    private IWechatService wechatService;

    @ApiOperationSupport(author = "lin")
    @ApiOperation(value = "查询用户信息加分页")
    @SaCheckLogin
    @SaCheckRole(mode = SaMode.OR, value = {"admin", "region-admin"})
    @PostMapping("/userList")
    public Result userList(@RequestBody WechatInquireVO wechatInquireVO) {
        if (StpUtil.hasPermission("2") && wechatInquireVO.getPosition() != null) {
            throw new PermissionsException();
        }
        Page<WechatInfoDTO> page = wechatService.inquireWechat(wechatInquireVO);
        List<WechatInfoDTO> records = page.getRecords();
        records.forEach(wechatInfoDTO -> {
            if (wechatInfoDTO.getPhone() != null) {
                wechatInfoDTO.setPhone(DesensitizedUtil.mobilePhone(wechatInfoDTO.getPhone()));
            }
        });
        return Result.success(MyPage.page(page)).msg("查询成功");
    }

    @ApiOperationSupport(author = "lin")
    @ApiOperation(value = "删除用户")
    @SaCheckLogin
    @SaCheckRole(mode = SaMode.OR, value = {"admin", "region-admin"})
    @PostMapping("/userDel/{id}")
    public Result userDel(@PathVariable Integer id) {
        Wechat wechat = wechatService.getById(id);
        if (wechat == null) {
            throw new NoWechatException();
        }
        wechatService.removeById(id);
        return Result.success().msg("删除成功");
    }

}
